﻿using System;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.ViewModels
{
    using Models;
    using Net;
    using Songhay.Models;
    using Songhay.Mvvm.Models;
    using Songhay.Mvvm.ViewModels;

    /// <summary>
    /// Defines the Client header targetValues data.
    /// </summary>
    public class HeaderViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HeaderViewModel"/> class.
        /// </summary>
        public HeaderViewModel()
        {
            if(base.IsInDesignMode)
            {
                this.ServerName = "Server Name";
                this.ServerNameUnc = this.ServerName;
                this.Title = "Design Mode Title";
                this.WindowsIdentityDisplayLine = "Windows Identity DisplayLine";
                return;
            }

            this.ServerName = "Looking for Server Name…";
            this.ServerNameUnc = this.ServerName;
            this.Title = "Loading…";
            this.WindowsIdentityDisplayLine = "Looking for Identity…";

            #region commanding:

            this.HeaderCommand = new RelayCommand<string>(param =>
            {

                if(this.CommandDictionary.ContainsKey(param))
                {
                    this.CommandDictionary[param].Invoke();
                    return;
                }

                var message = new LightNavigationMessage<string>(param, "HeaderCommand");
                Messenger.Default.Send(message);
            });

            #endregion

            #region messaging:

            var action = new Action<object, OpenReadCompletedEventArgs>((o, args) =>
            {
                if(args.Error != null) return; //TODO: handle error.

                var json = new DataContractJsonSerializer(typeof(HostServer));
                var data = json.ReadObject(args.Result) as HostServer;
                if(data != null)
                {
                    this.ServerName = data.ServerName;
                    this.ServerNameUnc = data.ServerNameUnc;
                    this.Title = data.Title;
                    this.WindowsIdentityDisplayLine = data.WindowsIdentityDisplayLine;

                    //Send MvvmLight message:
                    var message = new GenericMessage<HostServer>(data);
                    Messenger.Default.Send(message);
                }
            });

            Messenger.Default.Register(this, new Action<GenericMessage<ClientSettings>>(
            message =>
            {
                var uri = message.Content.JsonHeader;
                HttpVerbAsyncUtility.GetStream(uri, action);
            }));

            #endregion
        }

        /// <summary>
        /// Gets the header command.
        /// </summary>
        /// <value>The header command.</value>
        public ICommand HeaderCommand { get; private set; }

        /// <summary>
        /// Gets or sets the name of the server.
        /// </summary>
        /// <value>The name of the server.</value>
        public string ServerName
        {
            get { return _serverName; }
            set
            {
                _serverName = value;
                base.RaisePropertyChanged("ServerName");
            }
        }

        /// <summary>
        /// Gets or sets the server name in UNC format.
        /// </summary>
        /// <value>The server name (UNC).</value>
        public string ServerNameUnc
        {
            get { return _serverNameUnc; }
            set
            {
                _serverNameUnc = value;
                base.RaisePropertyChanged("ServerNameUnc");
            }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                base.RaisePropertyChanged("Title");
            }
        }

        /// <summary>
        /// Gets or sets the windows identity targetValues line.
        /// </summary>
        /// <value>The windows identity targetValues line.</value>
        public string WindowsIdentityDisplayLine
        {
            get { return _windowsIdentityDisplayLine; }
            set
            {
                _windowsIdentityDisplayLine = value;
                base.RaisePropertyChanged("WindowsIdentityDisplayLine");
            }
        }

        string _serverName;
        string _serverNameUnc;
        string _title;
        string _windowsIdentityDisplayLine;
    }
}
